'''
Created on 22 janv. 2013

@author: tclain
'''
from instance.Errors import *
class Container(object):
    '''
    Abstract class used to store componnets with multiple attributes from str representtaion
    '''
                   
    def __init__(self,rawstr,names,splitChar=':'):
        '''
        param : rawstr : the str storing information about the item
        param : names : a list containing  the name of the different attributes
        param : splitChar : the character that split information about the Item
        param : mandatoriesIdx : the field's index that are completely mandatory
        '''
        self._attributes = {}
        #creating the dict structure of attributes
        splitted = rawstr.split(splitChar)

        for k,n in enumerate(names) :
            self._attributes[n]= splitted[k] or ""
            
    def __str__(self):
        s = ""
        for k,v in self._attributes.iteritems():
            s+=k+":"+v+"\n"
        return s
    def get(self,attrName):
        '''
        Retrieve the attribute attrName
        '''
        if attrName in self._attributes :
            return self._attributes[attrName]
    def has(self,attrName):
        '''
        returns true if the attributes attrName exists or is not null
        '''
        return (attrName in self._attributes) and (self._attributes[attrName])
    
    
    
if __name__=='__main__':
    c = Container("test:a:c",["name","value","default"])
    print str(c)
                    